package com.gxa.shop.dao.account.impl;

import com.gxa.shop.bean.dto.AccountDto;
import com.gxa.shop.bean.pojo.Account;
import com.gxa.shop.bean.vo.AccountVo;
import com.gxa.shop.commons.jdbc.JDBCUtil;
import com.gxa.shop.dao.account.AccountDao;
import com.gxa.shop.bean.dto.Login;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AccountDaoImpl implements AccountDao {

    public Account login(Login login) {

        String sql="select * from  account where phone=? and password=? and role_id=?";
        JDBCUtil jdbcUtil=new JDBCUtil();
        if (login.getAccount().equals("15808477315")){
            login.setRoleId(1);
        }else {
            login.setRoleId(2);
        }
        List<Map<String, Object>> mapList = jdbcUtil.query(sql, login.getAccount(), login.getPassword(),login.getRoleId());
        if(mapList.size()>0){
            Account account=new Account();
            account.setUserId((Integer ) mapList.get(0).get("user_id"));
            account.setUsername((String)mapList.get(0).get("username"));
            account.setRoleId((Integer) mapList.get(0).get("role_id"));
            return  account;
        }
        return null;
    }

    @Override
    public int register(Account account) {
        String sql="insert into account (username,password,birth_date,gender,age,phone,email,role_id) values(?,?,?,?,?,?,?,?)";
        JDBCUtil jdbcUtil=new JDBCUtil();
        return jdbcUtil.update(sql,account.getUsername(),account.getPassword(),account.getBirthDate(),account.getGender()
        ,account.getAge(),account.getPhone(),account.getEmail(),account.getRoleId());
    }

    @Override
    public List<AccountVo> selectAll(AccountDto accountDto) {
        StringBuffer sql=new StringBuffer("SELECT a.user_id, r.role_name, a.username, a.password, a.birth_date, a.gender, a.age, a.phone, a.email " +
                " FROM account a  " +
                " JOIN role r ON a.role_id = r.role_id  " +
                " WHERE 1=1 ");
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Object> objects=new ArrayList<>();
        if(accountDto.getName()!=null){
            sql.append(" and a.username like concat ('%',?,'%') ");
            objects.add(accountDto.getName());

        }
        if(accountDto.getPhone()!=null){
            sql.append(" and a.phone like concat ('%',?,'%')");
            objects.add(accountDto.getPhone());
        }
        sql.append(" order  by a.user_id  desc limit " + (accountDto.getPage()-1)*accountDto.getLimit() + "," + accountDto.getLimit());
        List<Map<String,Object>> list=jdbcUtil.query(sql.toString(),objects.toArray());
        List<AccountVo> accountList=new ArrayList<>();
        for (Map<String,Object> map: list){
            AccountVo account=new AccountVo();
            account.setUserId((Integer) map.get("user_id"));
            account.setRoleId((String) map.get("role_name"));
            account.setUsername((String) map.get("username"));
            account.setPassword((String) map.get("password"));
            account.setBirthDate((Date) map.get("birth_date"));
            account.setGender((String) map.get("gender"));
            account.setAge((Integer) map.get("age"));
            account.setPhone((String) map.get("phone"));
            account.setEmail((String)map.get("email"));
            accountList.add(account);
        }
        return accountList;

    }

    @Override
    public int addAccount(Account account) {
        return 0;
    }


    @Override
    public int updateAccount(Account account) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql="update account set username=?,password=?,birth_date=?,gender=? ,age=?, phone=? , email=? ,role_id=? where user_id=?";
        return jdbcUtil.update(sql,account.getUsername(),account.getPassword(),account.getBirthDate(),
                account.getGender(),account.getAge(),account.getPhone(),account.getEmail(),account.getRoleId(),account.getUserId());
    }

    @Override
    public int deleteAccount(Integer id) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql=" DELETE FROM account WHERE user_id=?";
        return jdbcUtil.update(sql,id);
    }

    @Override
    public Long totalAccount(AccountDto accountDto) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        StringBuffer sql=new StringBuffer("SELECT count(*) as total    " +
                " FROM account a     " +
                "JOIN role r ON a.role_id = r.role_id     " +
                " WHERE 1=1" );
        List<Object> objects=new ArrayList<>();
        if(accountDto.getName()!=null){
            sql.append(" and a.username like concat ('%',?,'%') ");
            objects.add(accountDto.getName());

        }
        if(accountDto.getPhone()!=null){
            sql.append(" and a.phone like concat ('%',?,'%')");
            objects.add(accountDto.getPhone());
        }

        List<Map<String,Object>> mapList= jdbcUtil.query(sql.toString(),objects.toArray());
        Long ct = 0L;
        if (mapList.size() > 0) {
            //该变
            ct = (Long) mapList.get(0).get("total");
        }
        return ct;
    }

}
